var siteinfo = require("../../siteinfo.js");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        status_member: 0,
        room_info: [],
        start_date: '',
        start_time: '',
        end_date: '',
        end_time: '',
        cover_img: '',
        cover_img_url: '',
        share_img: '',
        share_img_url: '',
        is_real_name: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getRoomInfo();
        var startTime = new Date(parseInt(Date.parse(new Date()) + 720*1000));
        var start_date= startTime.getFullYear()+'-'+(startTime.getMonth() + 1)+'-'+startTime.getDate();
        var start_time= startTime.getHours()+':'+startTime.getMinutes();

        var endTime = new Date(parseInt(Date.parse(new Date()) + 3000*1000));
        var end_date= endTime.getFullYear()+'-'+(endTime.getMonth() + 1)+'-'+endTime.getDate();
        var end_time= endTime.getHours()+':'+endTime.getMinutes();

        this.setData({
            start_date: start_date,
            start_time: start_time,
            end_date: end_date,
            end_time: end_time,
        });
        let base_info = wx.getStorageSync('global_baseinfo')
        console.log(base_info.base_color)
        wx.setNavigationBarColor({
          backgroundColor: base_info.base_color,
          frontColor: '#000000',
        })
    },
    getRoomInfo: function () {
        var $this = this;
        wx.request({
            url: siteinfo.site_url + "api/live/getRoomInfo",
            data: {
                uniacid: siteinfo.uniacid,
                user_id: wx.getStorageSync("suid")
            },
            success: function (res) {
                if(res.data.data && res.data.data.is_living==1){
                    wx.showModal({
                        title: "提示",
                        content: '在上场预约直播结束时间前不可预约下场直播',
                        showCancel: !1,
                        confirmText: '确认',
                        success (res) {
                            if (res.confirm) {
                                wx.navigateTo({
                                    url: '/pages/usercenter/usercenter',
                                })
                            }
                        }
                    });
                }
                $this.setData({
                    room_info: res.data.data,
                    cover_img: res.data.data.cover_img,
                    cover_img_url: res.data.data.cover_img_url,
                    share_img: res.data.data.share_img,
                    share_img_url: res.data.data.share_img_url,
                });
            }
        })
    },
    bindPickerStartDate: function(e){
        this.setData({
            start_date: e.detail.value
        });
    },
    bindPickerStartTime: function(e){
        this.setData({
            start_time: e.detail.value
        });
    },
    bindPickerEndDate: function(e){
        this.setData({
            end_date: e.detail.value
        });
    },
    bindPickerEndTime: function(e){
        this.setData({
            end_time: e.detail.value
        });
    },
    cover_img_upload: function () {
        var $this = this;
        wx.chooseImage({
            count: 1,
            sizeType: ["original", "compressed"],
            sourceType: ["album", "camera"],
            success: function (e) {
                wx.showLoading({
                    title: "图片上传中"
                });
                var t = e.tempFilePaths;
                wx.uploadFile({
                    url: siteinfo.site_url + "api/live/upload",
                    filePath: t[0],
                    name: "file",
                    success: function (e) {
                        var data = JSON.parse(e.data);
                        $this.setData({
                            cover_img: data.data.path,
                            cover_img_url: data.data.url_path,
                        });
                        wx.hideLoading();
                    }
                })
            }
        })
    },
    share_img_upload: function () {
        var $this = this;
        wx.chooseImage({
            count: 1,
            sizeType: ["original", "compressed"],
            sourceType: ["album", "camera"],
            success: function (e) {
                wx.showLoading({
                    title: "图片上传中"
                });
                var t = e.tempFilePaths;
                wx.uploadFile({
                    url: siteinfo.site_url + "api/live/upload",
                    filePath: t[0],
                    name: "file",
                    success: function (e) {
                        var data = JSON.parse(e.data);
                        $this.setData({
                            share_img: data.data.path,
                            share_img_url: data.data.url_path,
                        });
                        wx.hideLoading()
                    }
                })
            }
        })
    },
    formSubmit: function (e) {
        wx.showLoading({
            title: '提交中',
        })
        var option = e.detail.value;
        option.uniacid = siteinfo.uniacid;
        option.user_id = wx.getStorageSync("suid");
        wx.request({
            url: siteinfo.site_url + "api/live/appointmentRoom",
            data: option,
            method: 'post',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if(res.data.code == 1){
                    wx.hideLoading();
                    wx.showModal({
                        title: "提示",
                        content: res.data.msg,
                        showCancel: !1,
                        confirmText: '确认',
                        success(res) {
                            if (res.confirm) {
                                wx.navigateTo({
                                    url: '/pages/usercenter/usercenter',
                                })
                            }
                        }
                    });
                }else{
                    wx.hideLoading({
                      complete: (res) => {},
                    })
                    if(res.data.data.errcode === 300002){
                        wx.showModal({
                            title: "提示",
                            content: '请检查参数长度!',
                            showCancel: !1,
                            confirmText: '确认'
                        });
                    }else{
                        wx.showModal({
                            title: "提示",
                            content: res.data.data.errmsg,
                            showCancel: !1,
                            confirmText: '确认'
                        });
                    }
                  
                }
            }
        })
    }
    
});